package com.kaiwukj.android.ufamily.mvp.ui.page.home.message.add;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;

import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.helper.AccountStore;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ChatSearchPersonResult;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class PersonResultAdapter extends BaseQuickAdapter<ChatSearchPersonResult, BaseViewHolder> {
    public PersonResultAdapter() {
        super(R.layout.item_chat_search_person_result, new ArrayList<>());
        addChildClickViewIds(R.id.iv_avatar, R.id.btn_attention);
    }

    public void postAttention(int position) {
        getData().get(position).setConcern(1);
        notifyItemChanged(position, "attention");
    }

    public void postCancelAttention(int position) {
        getData().get(position).setConcern(0);
        notifyItemChanged(position, "attention");
    }

    @Override
    public void onBindViewHolder(@NotNull BaseViewHolder holder, int position, @NotNull List<Object> payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            String tag = (String) payloads.get(0);
            if ("attention".equals(tag)) {
                QMUIRoundButton btnAttention = holder.getView(R.id.btn_attention);
                if (getItem(position).isConcern()) {
                    //btnAttention.setBackgroundColor(Color.parseColor("#f2f2f2"));
                    btnAttention.setText("已关注");
                } else {
                   // btnAttention.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
                    btnAttention.setText("关注");
                }
            }
        }
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, ChatSearchPersonResult result) {
        ImageView ivAvatar = holder.getView(R.id.iv_avatar);
        Glide.with(getContext())
                .load(result.getHeadImg())
                .into(ivAvatar);
        holder.setText(R.id.tv_name, result.getNickName());
        holder.setText(R.id.tv_desc, result.getSignature());
        QMUIRoundButton btnAttention = holder.getView(R.id.btn_attention);

        if (result.getUserId().equals(AccountStore.getUserResult().getId())) {
            btnAttention.setVisibility(View.GONE);
        } else {
            if (result.isConcern()) {
                btnAttention.setBackgroundColor(Color.parseColor("#f2f2f2"));
                btnAttention.setText("已关注");
            } else {
                btnAttention.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
                btnAttention.setText("关注");
            }
        }


    }

}
